package com.example.ww.bleapp.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.example.ww.bleapp.R;

import java.util.ArrayList;
import java.util.List;

public class BlutoothListAdapter extends RecyclerView.Adapter<BlutoothListAdapter.VH> {

    private Context context;
    private List<BleDevice> bleDeviceList = new ArrayList<>();
    private OnItemClickListener mListener;

    public BlutoothListAdapter(Context context, List<BleDevice> bleDeviceList) {
        this.context = context;
        this.bleDeviceList = bleDeviceList;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(context).inflate(R.layout.recyclerview_item, viewGroup, false);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(@NonNull VH vh, final int i) {
        vh.nameT.setText(bleDeviceList.get(i).getName() + "--" + bleDeviceList.get(i).getMac());
        if(mListener!=null){
            vh.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onClick(i);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return bleDeviceList.size();
    }

    public class VH extends RecyclerView.ViewHolder {

        TextView nameT;

        public VH(@NonNull View itemView) {
            super(itemView);
            nameT = itemView.findViewById(R.id.name);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);


    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mListener = onItemClickListener;
    }
}
